package com.fastloan.safe.mgt.login.controller;

import com.fastloan.safe.mgt.login.service.LoginService;
import com.kawins.result.ResultTo;
import com.kawins.shiro.temp.core.result.ShiroResultEnum;
import com.kawins.shiro.temp.util.ShiroKit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 登陆登出Controller
 *
 * @author rankai
 *         createTime 2017-09-2017/9/5 17:01
 */
@RestController
@RequestMapping("/api/logon")
public class LogonController {

    @Autowired
    private LoginService loginService;

    /**
     * 登陆实现
     *
     * @param request
     * @param type    -1:客户登陆,null:用户web的登陆,不为null和-1:限制角色登陆(roleType)
     * @return
     */
    @PostMapping(value = "/login")
    public ResultTo login(HttpServletRequest request, @RequestParam(required = false) Integer type) {
        return loginService.login(request, type);
    }

    /**
     * 退出
     */
    @GetMapping("/noLogin")
    public ResultTo noLogin() {
        return new ResultTo(ShiroResultEnum.NO_LOGIN.getCode(), ShiroResultEnum.NO_LOGIN.getMessage());
    }

    /**
     * 退出
     */
    @GetMapping("/out")
    public ResultTo loginOut() {
        ShiroKit.logout();
        return new ResultTo();
    }

    /**
     * 发送验证码或者手机号
     *
     * @return
     */
    @PostMapping("/sendLoginCode")
    public ResultTo sendCode(String value) {
        return loginService.sendCode(value);
    }

    /**
     * 判断是否登陆
     *
     * @param
     * @return
     */
    @GetMapping("/isLogin")
    public ResultTo isLogin() {
        boolean authenticated = ShiroKit.getSubject().isAuthenticated();
        if (authenticated) {
            return new ResultTo().setData(Boolean.TRUE);
        }
        return new ResultTo().setData(Boolean.FALSE);
    }
}
